package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.BaseAttributeMapper;
import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		List<JSONObject> list = new ArrayList<>();

		Map<Long, List<BaseCategoryView>> basecategory1View = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		basecategory1View.entrySet().forEach(entry -> {
			// 一级分类封装对象
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("categoryId", entry.getKey());
			jsonObject.put("categoryName", entry.getValue().get(0).getCategory1Name());
			Map<Long, List<BaseCategoryView>> category2View = entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			List<JSONObject> category2List = new ArrayList<>();
			category2View.entrySet().forEach(entry2 -> {
				// 二级分类分装对象
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry2.getKey());
				jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());
				Map<Long, List<BaseCategoryView>> category3View = entry2.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
				List<JSONObject> category3List = new ArrayList<>();
				category3View.entrySet().forEach(entry3 -> {
					// 三级分类封装对象
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", entry3.getKey());
					jsonObject3.put("categoryName", entry3.getValue().get(0).getCategory3Name());
					category3List.add(jsonObject3);
				});
				jsonObject2.put("categoryChild", category3List);
				category2List.add(jsonObject2);
			});
			jsonObject.put("categoryChild", category2List);
			list.add(jsonObject);
		});
		return list;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.findAttribute(category1Id);
	}
}
